package org.opens;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.io.IoUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Test;
import org.opens.excel.entity.ForEachTemplateEntity;
import org.opens.util.EntityRandomUtil;
import org.opens.util.PathUtil;
import org.opens.util.ZipUtil;

import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ForEachTemplateExport {

    /**
     * 测试导出excel模板中使用fe标签遍历List
     */
    @Test
    public void testForEachExport() throws InstantiationException, IllegalAccessException {
        Map<String, Object> param = new HashMap<>();

        List<ForEachTemplateEntity> list = EntityRandomUtil.getList(ForEachTemplateEntity.class, 5);

        param.put("title", "测试easypoi的foreach导出");
        param.put("list", list);

        System.out.println(list);

        String currentClassPath = PathUtil.getCurrentClassRunPath();
        TemplateExportParams templateExportParams = new TemplateExportParams(PathUtil.pathSplicing(currentClassPath, "ForEachTemplate.xlsx"));

        Workbook workbook = null;

        FileOutputStream fileOutputStream = null;
        try {
            workbook = ExcelExportUtil.exportExcel(templateExportParams, param);
            fileOutputStream = new FileOutputStream(new File(currentClassPath, "ForEachTemplateOut.xlsx"));
            workbook.write(fileOutputStream);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            IoUtil.close(fileOutputStream);
            IoUtil.close(workbook);
        }

    }

    @Test
    public void exportToZip() {
        String zipFilePath = "C:\\Users\\99126\\Desktop\\temp\\00001\\aaa.zip";
        try (ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipFilePath))) {
            for (int i = 0; i < 5; i++) {
                zipOutputStream.putNextEntry(new ZipEntry("/excel/random" + i + ".xls"));
                Workbook randomWorkBook = getRandomWorkBook();
                randomWorkBook.write(zipOutputStream);
                IoUtil.close(randomWorkBook);
            }
            zipOutputStream.finish();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Workbook getRandomWorkBook() throws Exception {
        Map<String, Object> param = new HashMap<>();

        List<ForEachTemplateEntity> list = EntityRandomUtil.getList(ForEachTemplateEntity.class, 5);

        param.put("title", "测试easypoi的foreach导出");
        param.put("list", list);

        System.out.println(list);

        String currentClassPath = PathUtil.getCurrentClassRunPath();
        TemplateExportParams templateExportParams = new TemplateExportParams(PathUtil.pathSplicing(currentClassPath, "ForEachTemplate.xlsx"));

        return ExcelExportUtil.exportExcel(templateExportParams, param);
    }

}
